package com.alan.camunda.boot.groovymod.language;

import com.alan.camunda.boot.util.GroovyUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class LanguageGeneral {

    private static GroovyUtil groovyUtil;

    private static String userJsonStr = """
            {
              "name": "zhangsan",
               "age": 20,
               "interests": ["A",  "B",  "C" ],
               "department": {
                 "deptId": "d0001",
                 "deptName": "HR"
               }
            }
            """;

    private static String tableStr = """
           [
             {
                "name": "user1",
                "age": 20
             },
             {
                "name": "user2",
                "age": 22
             },
             {
                "name": "user3",
                "age": 23
             }
           ]
           """;

    public static void main(String[] args) {

        Map<String, Object> variables = new HashMap<>();
        JSONObject failedInfo = new JSONObject();
        failedInfo.put("code", 3333);
        failedInfo.put("msg", "接口调用失败");

        variables.put("failedInfo", failedInfo);

        variables.put("user", JSONObject.parse(userJsonStr));
        variables.put("table", JSONArray.parse(tableStr));

        groovyUtil = new GroovyUtil(variables);

        log.info("参数为 {}", JSON.toJSONString(variables));

        execute( "failedInfo.code");
        execute( "failedInfo['code']");
        execute( "3.2 + 3.2");
        execute( "0.1 + 0.2");
        execute( "3 - 2");
        execute( "3 * 2");
        execute( "6 / 2");
        execute( "3 / 2");
        execute( "'3' + 3.2");
        execute( "'hello' + ' ' + 'world'");
        execute("""
                "hello" + " " + "world"
                """);

        execute( "30 > 10");
        execute( "3 > 1.5");
        execute( "30 == 10");
        execute( "30 != 10");
        execute( "!(2 > 1)");

        execute("(1 > 0 ) && (2 > 0)");
        execute("(1 > 0 ) && (2 < 0)");
        execute("(1 > 0 ) || (2 < 0)");


        execute("user.name");
        execute("user['name']");
        execute("user['interests']");
        execute("user['interests'][0]");


        execute("table[0]['name']");
        execute("table[1]['name']");
        execute("table[0]['age']");
        execute("table['name']");
        execute("table['name'][0]");
        execute("table['name'][1]");


    }



    private static void execute(String expression) {
        log.info("{}  ----> {}", expression, groovyUtil.getValue(expression));
    }



}
